import java.util.Deque;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2023-02-07
 * Time: 11:55
 */
public class Sort {
    /**
     * 插入排序
     *
     * 时间复杂度：最坏情况O(N^2)
     *          最好情况O(N)，当数据越趋于有序时，排序速度越快
     *              也就是说，直接插入排序一般使用场景就是数据基本有序的时候
     * 空间复杂度：O(1)
     * 稳定性：稳定
     *
     * 待排序数组
     * @param array
     */
    public static void insertionSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if(tmp < array[j]) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    /**
     * 交换数组中的两个数据
     *
     * 待操作数组
     * @param array
     * 两个数组下标
     * @param j
     * @param i
     */
    private static void swap(int[] array, int j, int i) {
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }

    /**
     * 希尔排序
     *
     * 时间复杂度：O(N^1.3) - O(N^1.5) 无法精准确定
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     *
     * 待排序数组
     * @param array
     */
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }
    }
    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                   array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    /**
     * 选择排序
     *
     * 时间复杂度：O(N^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     *
     * 待排序数组
     * @param array
     */
    public static void selectionSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            int j = i + 1;
            for (; j < array.length; j++) {
                if(array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }
    public static void selectionSort1(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int j = left + 1; j <= right; j++) {
                if(array[j] > array[maxIndex]) {
                    maxIndex = j;
                }
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,left,minIndex);
            /*为了防止最大值在left的位置上，这时候最大值会被交换到minIndex的位置上
            所以应该使maxIndex等于minIndex*/
            if(left == maxIndex) {
                maxIndex = minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

    /**
     * 堆排序
     *
     * 时间复杂度：O(N*logN)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     *
     * @param array
     */
    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while (end > 0) {
            swap(array, 0, end);
            downwardAdjustment(array, 0, end);
            end--;
        }
    }
    //以大根堆的方式排序
    private static void createBigHeap(int[] array) {
        //找到最后一个节点的父亲节点
        int parents = (array.length-1-1)/2;
        for (; parents >= 0; parents--) {
            downwardAdjustment(array, parents, array.length);
        }
    }
    //向下调整
    private static void downwardAdjustment(int[] array, int parents, int len) {
        int child = 2*parents+1;
        while (child < len) {
            if(child + 1 < len && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parents]) {
                swap(array, child, parents);
                parents = child;
                child = 2*parents + 1;
            }else {
                break;
            }
        }
    }

    /**
     * 冒泡排序
     *
     * 时间复杂度：最坏情况O(N^2),优化后最好情况可达到O(N)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     *
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean flag = true;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    flag = false;
                    swap(array, j, j+1);
                }
            }
            if(flag) {
                return;
            }
        }
    }

    /**
     *快速排序
     *
     * 时间复杂度：O(N*logN)
     *          最好情况：O(N*logN)
     *          最坏情况：数据有序时，树的高度为N，O(N^2) 所以要进行优化！！
     * 空间复杂度：最好情况：O(logN)
     *            最坏情况：O(N)
     * 稳定性：不稳定
     *
     * @param array
     */
    public static void quickSort(int[] array) {
        quick(array, 0, array.length-1);
    }
    private static void quick(int[] array, int left, int right) {
        if(left >= right) {
            return;
        }
        //优化一：当数组元素少时，可以不用继续递归，且此时数组已经趋于有序了，可以直接使用插入排序
        if(left-right + 1< 3) {
            insertionSort2(array, left ,right);
            return;
        }
        //优化二：三数取中法优化
        int k = midThree(array, left, right);
        swap(array, k, left);
        int pivot = partition2(array, left, right);
        quick(array, left, pivot-1);
        quick(array, pivot+1, right);
    }
    public static void insertionSort2(int[] array, int left, int right) {
        for (int i = left+1; i <= right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if(tmp < array[j]) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    //三数取中法
    private static int midThree(int[] array, int left, int right) {
        int mid = (left+right)/2;
        if(array[left] < array[right]) {
            if(array[mid] > array[right]) {
                return right;
            }else if(array[mid] < array[left]) {
                return left;
            }else {
                return mid;
            }
        }else {
            if(array[mid] < array[right]) {
                return right;
            }else if(array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }
    //挖坑法划分
    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            if(array[right] < tmp) {
                array[left] = array[right];
                break;
            }else {
                right--;
            }
        }
        while (left < right) {
            if(array[left] > tmp) {
                array[right] = array[left];
                break;
            }else {
                left++;
            }
        }
        array[left] = tmp;
        return left;
    }
    //Hoare法划分
    private static int partition1(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        //这里只能让右边先走，再走左边，有可能会把比基准值大的值放到数组前面。
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                    right--;
            }
            //这里必须写array[left] <= tmp,不能写array[left] < tmp
            //这样会导致第一次left永远都不会移动
            while (left < right && array[left] <= tmp) {
                    left++;
            }
            swap(array, left, right);
        }
        swap(array, left, i);
        return left;
    }
    //前后指针法
    private static int partition2(int[] array, int left, int right) {
        int prev = left;
        int cur = prev+1;
        while (cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array, prev, cur);
            }
            cur++;
        }
        swap(array, prev, left);
        return prev;
    }

    /**
     * 非递归实现快速排序
     *
     * 时间复杂度：O(N*logN)
     * 空间复杂度：O(logN)
     * 稳定性：不稳定
     *
     *@param array
     */
    public static void quickSort2(int[] array) {
        Deque<Integer> stack = new LinkedList<>();
        int left = 0;
        int right = array.length-1;
        int pivot = partition2(array, left, right);
        if(pivot-1 > left) {
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot+1 < right) {
            stack.push(pivot+1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partition2(array, left, right);
            if(pivot-1 > left) {
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot+1 < right) {
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    /**
     * 归并排序
     *
     * 时间复杂度：O(N*logN)
     * 空间复杂度：O(N)
     * 稳定性：稳定
     *
     * @param array
     */
    public static void mergeSort(int[] array) {
        int left = 0;
        int right = array.length-1;
        mergeSortFunc(array, left, right);
    }
    private static void mergeSortFunc(int[]array, int left, int right) {
        if(left >= right) {
            return;
        }
        int mid = (left+right)/2;
        mergeSortFunc(array, left, mid);
        mergeSortFunc(array, mid+1, right);
        merge(array, mid, left ,right);
    }
    private static void merge(int[]array, int mid, int left, int right) {
        int s1 = left;
        int s2 = mid+1;
        int n = right-left + 1;
        int[] tmp = new int[n];
        int k = 0;
        while (s1 <= mid && s2 <= right) {
            if(array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        //这里要用while，不能用if ，因为可能有多个数据
        while (s1 <= mid) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= right) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < tmp.length; i++) {
            array[i+left] = tmp[i];
        }
    }

    /**
     * 非递归实现归并排序
     *
     * 时间复杂度：O(N*logN)
     * 空间复杂度：O(N)
     * 稳定性：稳定
     *
     * @param array
     */
    public static void mergeSort1(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i += 2 * gap) {
                int left = i;
                int mid = left+gap-1;//有可能越界
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid+gap;//有可能越界
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array, mid, left, right);
            }
            gap *= 2;
        }
    }

    /**
     * 计数排序
     *
     * 时间复杂度：O(N+范围)
     * 空间复杂度：O(范围)
     * 稳定性：不稳定
     *
     * 适用于一组数据集中在一定范围内
     *
     * @param array
     */
    public static void countingSort(int[] array) {
        int min = array[0];
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(array[i] < min) {
                min = array[i];
            }
            if(array[i] > max) {
                max = array[i];
            }
        }
        int len = max-min+1;
        int[] count = new int[len];
        for (int i = 0; i < array.length; i++) {
            count[array[i] - min]++;
        }
        int j = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                array[j++] = i + min;
                count[i]--;
            }
        }
    }

}
